package org.spring.aicloud;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.spring.aicloud.entity.Discuss;
import org.spring.aicloud.service.IDiscussService;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.StampedLock;

@SpringBootTest
public class MpPageTest {

    @Resource
    private IDiscussService discussService;

    @Test
    void test() {
        Page<Discuss> page = discussService.page(new Page<>(4, 2));
        System.out.println(page);
    }

    public static void main(String[] args) throws InterruptedException {
        final StampedLock lock = new StampedLock();
        Thread thread = new Thread(() -> {
            // 获取写锁
            lock.writeLock();
            // 永远阻塞在此处，不释放写锁
            LockSupport.park();
        });
        thread.start();
        // 保证 thread 获取写锁
        Thread.sleep(100);
        Thread thread2 = new Thread(() ->
                // 阻塞在悲观读锁
                lock.readLock()
            );
        thread2.start();
        // 保证 thread2 阻塞在读锁
        Thread.sleep(100);
        // 中断线程 thread2，导致 thread2 CPU 飙升
        thread2.interrupt();
        thread2.join();
    }

}
